﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using OF.Component.Base;
using OF.Utility;

namespace OP.manage.ajax
{
    public partial class UploadHandler1 : System.Web.UI.Page
    {
        protected string szBasePath, szShowBasePath, szFileName, szExtName, szFilePath, szShowPath, szReturn;
        protected string szReturnFormat = "{{\"IsSuccess\":\"{0}\",\"Message\":\"{1}\",\"Width\":\"{2}\",\"Height\":\"{3}\"}}";

        public string UpLoadImage
        {
            get
            {
                string strpath = RequestUtility.GetMapPathString() + SiteUtility.GetAppSettings("UpLoadImage");
                if (SiteUtility.GetAppSettings("UpLoadImage").IndexOf(":") > -1)
                {
                    strpath = SiteUtility.GetAppSettings("UpLoadImage");
                }

                return strpath;
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            switch (Request["action"])
            {
               
                case "uploadArticleTitleImg":
                    string dirName = SiteUtility.GetAppSettings("ArticleTitleImgDir-Name");
                    UploadImg(dirName);
                    break;
                default:
                    break;
            }
        }

      
        /// <summary>
        /// 上传图片公用方法
        /// </summary>
        /// <param name="dirName"></param>
        private void UploadImg(string dirName)
        {
            HttpPostedFile fudCommon = Request.Files[0];
            szReturn = AjaxResult.Error("上传失败").ToString();
            CarImageReturn oReturn = new CarImageReturn();
            //int mwidth = EConvert.ToInt(SiteUtility.GetAppSettings("UpLoadImageMW"));
            //int mheight = EConvert.ToInt(SiteUtility.GetAppSettings("UpLoadImageMH"));
            //int pcwidth = EConvert.ToInt(SiteUtility.GetAppSettings("UpLoadImagePcW"));
            //int pcheight = EConvert.ToInt(SiteUtility.GetAppSettings("UpLoadImagePcH"));
            //int beishu = EConvert.ToInt(SiteUtility.GetAppSettings("UpLoadImageBeishu"));

           
            string strName = "";
            string fileName, fileExtension, filesize;
            fileName = System.IO.Path.GetFileName(fudCommon.FileName.ToString()); //得到文件名
            filesize = System.IO.Path.GetFileName(fudCommon.ContentLength.ToString()); //得到文件大小



            fileExtension = Path.GetExtension(fudCommon.FileName);//'获取扩展名
            
            string fileTrueName = FileUtility.GetFileName(fileExtension);
            var dirPath = Path.Combine(UpLoadImage, dirName);
            FileUtility.CreateDirectory(dirPath);
            string httpImgUrl = CommonPage.ImagesUrl+"/"+dirName+"/"+fileTrueName;
            try
            {
                var filePath = Path.Combine(dirPath, fileTrueName);
                fudCommon.SaveAs(filePath);
                szReturn = AjaxResult.Success(new CarImageReturn
                {
                    ImgUrl = dirName + "/" + fileTrueName,
                    PhysicalImgUrl = filePath,
                    HttpImgUrl = httpImgUrl,
                    Width = 0,
                    Height = 0,
                    Size = Convert.ToDecimal(fudCommon.ContentLength / 1024),
                    ImgUploadName = fudCommon.FileName
                }).ToString();
            }
            catch (Exception ex)
            {
                szReturn = string.Format(szReturnFormat, "false", "上传文件时发生异常" + ex.Message, 0, 0);
            }

            Response.Write(szReturn);

            /* return ImagesUrl;*/
        }
    }

    public class CarImageReturn
    {
        /// <summary>
        /// 图片显示名称
        /// </summary>
        public string ImgUrl { get; set; }
        /// <summary>
        /// 图片相对物理路径地址
        /// </summary>
        public string PhysicalImgUrl { get; set; }
        /// <summary>
        /// 图片引用名称，即显示给前端显示的名称
        /// </summary>
        public string HttpImgUrl { get; set; }
        public int Width { get; set; }
        public int Height { get; set; }
        public decimal Size { get; set; }
        /// <summary>
        /// 上传时图片的名称
        /// </summary>
        public string ImgUploadName { get; set; }

    }
}